import { userGetInfoService } from "@/api/user"
import { defineStore } from "pinia"
import {ref} from 'vue'

// 导出的useUserStore是一个封装好的函数,不是对象
export const useUserStore = defineStore('user', () => {
  const token = ref('')
  const setToken = (newToken) => {
    token.value = newToken
  }
  const removeToken = () => {
    token.value = ''
  }

  // 获取用户基本信息
  const user = ref({})
  const getUser = async () => {
    const res = await userGetInfoService()
    user.value = res.data.data
  }

  const setUser = (obj) => {
    user.value = obj
  }

  return {
    token,
    setToken,
    removeToken,
    user,
    getUser,
    setUser
  }
},
  {
    persist: true
  }
)